<?php
/**
 * Description: 快速智能练习
 * User: devforma
 * Date: 15/4/8
 * Time: 11:30
 */
require __DIR__ . '/includes/utils.php';
class AutoTraining implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;
        
        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        $userId = $queryArgs['user_id'];

        //获取知识点id
        $userData = getUserData($userId);
        if (empty($userData))
            return genOutput(1000);

        $noteIds = array_keys(normalizeUserNoteInfo($userData->note_info));

        //根据知识点抽题
        $questions = getNoteQuestions($noteIds, 15, $userId, true);
        
        $groupOrder = array();
        foreach ($questions as &$q) {
            switch ($q['category_name']) {
                case '常识判断':
                    $groupOrder[1][] = $q; 
                    break;
    
                case '言语理解与表达':
                    $groupOrder[2][] = $q;
                    break;
                
                case '数量关系':
                    $groupOrder[3][] = $q;
                    break;
    
                case '判断推理':
                    $groupOrder[4][] = $q;
                    break;
    
                case '资料分析':
                    $groupOrder[5][] = $q;
            }	
        }
        unset($questions);
    
        $qids = array();
        ksort($groupOrder);
        foreach ($groupOrder as &$group) {
            foreach ($group as &$q) {
                $questions[] = $q;
                $qids[] = $q['id'];
            }
        }

        $new = array(
            'user_id' => $userId,
            'paper_id' => 0,
            'paper_type' => getPaperType('快速练习')->type_id,
            'paper_title' => '快速练习',
            'question_info' => implode(',', $qids),
            'create_time' => date('Y-m-d H:i:s'),
            'status' => 0,
            'duration' => 0,
            'right_count' => 0,
            'total_count' => 0,
            'terminal_type' => empty($_GET['terminal_type']) ? '' : trim($_GET['terminal_type'])
        );
        $result = $cmsdb->insert('qb_relationship_user_paper', $new);

        if (empty($result)) {
            return genOutput(1001);
        }

        return genOutput(1, array(
            'paper_id' => intval($cmsdb->lastInsertId()),
            'duration' => 15*60,
            'questions' => $questions
        ));
    }
}